<?php
namespace Admin\Controller;
use Think\Controller;

class MessageController extends HomeController {

	/*
	* Function:		CourseCancelList
	* Description:	课程取消列表
	*/
    public function courseCancelList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter = returnFilter($filter);
        $filter['status'] = 3;
		$list = D('UserCourse')->getUserCourseList($page,$filter,$pageSize);
		if(is_array($list)&&count($list)>0){
		    foreach($list as $key=>&$val){
		        $val['store_name'] = M('stores_list')->where(['id'=>$val['store_id']])->getField('title');
		        $val['user_name'] = M('user')->where(['id'=>$val['user_id']])->getField('username');
		    }
		}
		$count =  D('UserCourse')->getUserCourseCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
        $storeList = D('Stores')->getStoresAllList();
        $this->assign('storeList',$storeList);
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }

    /*
	* Function:		cardValidList
	* Description:	会员卡有效期列表
	*/
    public function cardValidList(){
        $filter = I('param.');
        $page =  I('get.p',1);
        $pageSize = 10;
        $filter = returnFilter($filter);
        $list = D('UserCard')->getUserCardValidList($page,$filter,$pageSize);
        if(is_array($list)&&count($list)>0){
            foreach ($list as $key=>&$val){
                $val['store_name'] = M('stores_list')->where(['id'=>$val['store_id']])->getField('title');
                $user_name = M('user')->where(['id'=>$val['user_id']])->getField('username');
                $val['user_name'] = emojiDecode($user_name);
                $val['goods_title'] = M('goods')->where(['id'=>$val['goods_id']])->getField('goods_title');
            }
        }
        $count =  D('UserCard')->getUserCardCount($filter);
        $pages = getpage($count,$pageSize,$filter);
        $show = $pages->show();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->assign('filter',$filter);
        $this->display();
    }

    /*
	* Function:		cardBalanceList
	* Description:	会员卡余额列表
	*/
    public function cardBalanceList(){
        $filter = I('param.');
        $page =  I('get.p',1);
        $pageSize = 10;
        $filter = returnFilter($filter);
        $list = D('UserCard')->getUserCardList($page,$filter,$pageSize);
        if(is_array($list)&&count($list)>0){
            foreach ($list as $key=>&$val){
                $val['store_name'] = M('stores_list')->where(['id'=>$val['store_id']])->getField('title');
                $user_name = M('user')->where(['id'=>$val['user_id']])->getField('username');
                $val['user_name'] = emojiDecode($user_name);
                $val['goods_title'] = M('goods')->where(['id'=>$val['goods_id']])->getField('goods_title');
            }
        }
        $count =  D('UserCard')->getUserCardCount($filter);
        $pages = getpage($count,$pageSize,$filter);
        $show = $pages->show();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->assign('filter',$filter);
        $this->display();
    }
	
	/*
	* Function:		addCourse
	* Description:	添加课程
	*/
	public function addCourse(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			if(empty($data['level'])){
			    $data['level'] = 3;
			}
			$data['content'] = stripslashes(htmlspecialchars_decode($data['content']));
			if(D('Course')->addCourse($data)){
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加失败!');
				$this->ajaxReturn($ret);
			}
		}else{
		    $cateList = D('CourseCate')->getCourseCateAllList();
		    $this->assign('cateList',$cateList);
		    $coachList = M('stores_worker_list')->where(['role'=>1,'status'=>1,'is_deleted'=>0])->select();
		    $this->assign('coachList',$coachList);
		    $classRoomList = M('stores_classroom_list')->where(['status'=>1,'is_deleted'=>0])->select();
		    $this->assign('classRoomList',$classRoomList);
			$this->display();
		}
	}
	
	/*
	* Function:		editCourse
	* Description:	编辑课程
	* @param:		$id int id
	*/
	public function editCourse($id = ''){
		if(IS_POST){
			$data = I('post.');
			$data['content'] = stripslashes(htmlspecialchars_decode($data['content']));
			$ret = D('Course')->updateCourse($data);
			if($ret!==false){
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$info = D('Course')->getCourseInfo($id);
				$this->assign('info',$info);
				$cateList = D('CourseCate')->getCourseCateAllList();
    		    $this->assign('cateList',$cateList);
    		    $coachList = M('stores_worker_list')->where(['role'=>1,'status'=>1,'is_deleted'=>0])->select();
    		    $this->assign('coachList',$coachList);
    		    $classRoomList = M('stores_classroom_list')->where(['status'=>1,'is_deleted'=>0])->select();
    		    $this->assign('classRoomList',$classRoomList);
				$this->display();
			}
		}
	}
	
	/*
	* Function:		deleteCourse
	* Description:	删除课程
	* @param:		$id
	*/
	public function deleteCourse($id = ''){
		if(M('course')->where(['id'=>$id])->delete()){
			$ret = array('code'=>1,'message'=>'操作成功');
			$this->ajaxReturn($ret);
		}else{
			$ret = array('code'=>0,'message'=>'操作失败!');
			$this->ajaxReturn($ret);
		}
	}
	
	/*
	* Function:		courseCateList
	* Description:	课程分类列表
	*/
    public function courseCateList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter = returnFilter($filter);
		$list = D('CourseCate')->getCourseCateList($page,$filter,$pageSize);
		if(is_array($list)&&count($list)>0){
		    foreach($list as $key=>&$val){
		        $val['store_name'] = M('stores_list')->where(['id'=>$val['store_id']])->getField('title');
		    }
		}
		$count =  D('CourseCate')->getCourseCateCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }
	
	/*
	* Function:		addCourseCate
	* Description:	添加课程分类
	*/
	public function addCourseCate(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			if(D('CourseCate')->addCourseCate($data)){
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$this->display();
		}
	}
	
	/*
	* Function:		editCourseCate
	* Description:	编辑课程分类
	* @param:		$id int id
	*/
	public function editCourseCate($id = ''){
		if(IS_POST){
			$data = I('post.');
			$ret = D('CourseCate')->updateCourseCate($data);
			if($ret!==false){
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$info = D('CourseCate')->getCourseCateInfo($id);
				$this->assign('info',$info);
				$this->display();
			}
		}
	}
	
	/*
	* Function:		deleteCourseCate
	* Description:	删除课程分类
	* @param:		$id
	*/
	public function deleteCourseCate($id = ''){
	    if(!$info = D('CourseCate')->getCourseCateInfo($id)){
		    $this->ajaxReturn(['code'=>0,'message'=>'不存在此记录']);
		}
		$ret = M('course_cate')->where(['id'=>$id])->save(['is_deleted'=>1]);
		if($ret!==false){
			$ret = array('code'=>1,'message'=>'操作成功');
			$this->ajaxReturn($ret);
		}else{
			$ret = array('code'=>0,'message'=>'操作失败!');
			$this->ajaxReturn($ret);
		}
	}
	
	/*
	* Function:		courseScheduleList
	* Description:	课程表-日/周
	*/
    public function courseScheduleList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter = returnFilter($filter);
		$list = D('Course')->getCourseList($page,$filter,$pageSize);
		if(is_array($list)&&count($list)>0){
		    foreach($list as $key=>&$val){
		        $val['store_name'] = M('store_list')->where(['id'=>$val['store_id']])->getField('title');
		    }
		}
		$count =  D('Course')->getCourseCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }
}